package com.media.themovieworld.screen.main;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.media.themovieworld.R;
import com.media.themovieworld.api.ApiParams;
import com.media.themovieworld.api.RequestModel;
import com.media.themovieworld.common.ListMovieScreen;
import com.media.themovieworld.common.adapter.CategoryAdapter;
import com.media.themovieworld.common.fragment.MovieTaskScreen;
import com.media.themovieworld.model.CategoryModel;
import com.media.themovieworld.model.ObServerListener;
import com.media.themovieworld.util.AppSettingPrefference;



public class CategoryScreen extends MovieTaskScreen implements ObServerListener{

	private GridView mGridView;
	private CategoryAdapter mAdapter;
	private View root;
	private ArrayList<CategoryModel> mListDatas;
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		root = inflater.inflate(R.layout.list_cate_screen, container,false);
		mGridView = (GridView) root.findViewById(R.id.grid);
		loadData();
		return root;
	}
	
	public void createUI(){
		mAdapter = new CategoryAdapter(getActivity(), mListDatas);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ListMovieScreen listMovieScreen = new ListMovieScreen();
				listMovieScreen.setCateId(mListDatas.get(arg2).categoryId);
				listMovieScreen.setTitle(mListDatas.get(arg2).name);
				getMainActivity().switchContent(listMovieScreen, true);
			}
		});
	}
	
	public void loadData(){
		final RequestModel requestData = ApiParams.getCategoryParams("", "0",AppSettingPrefference.getInstance().getMovieLanguage()); 
//		final SoapRequestModel requestData = MovieWorldApi.getMovieCategoryRequest(getActivity());
		executeSoapTaskReturnList(requestData, true, CategoryModel.class, new MovieTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				if (object!=null) {
					removeRetryView();
					mListDatas = (ArrayList<CategoryModel>) object;
					createUI();
				}else{
					addRetryView();
				}
			}
		});
	}
	
	@Override
	public void onRetry() {
		removeRetryView();
		loadData();
	}

	@Override
	public void update(Object ob) {
		onRetry();
	}
	
}
